package com.skyworth.web.admin.role.service.impl;

import com.google.common.collect.Lists;
import com.skyworth.web.admin.role.domain.RoleModel;
import com.skyworth.web.admin.role.form.RoleForm;
import com.skyworth.web.admin.role.repository.RoleRepo;
import com.skyworth.web.admin.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by skyworth on 2016/9/22.
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleRepo roleRepo;

	@Override
	public List<RoleModel> listRole(long store_id) {
		return Lists.newArrayList(roleRepo.findByStoreId(store_id));
	}

	@Override
	public Page<RoleModel> pageRole(long store_id, Pageable pageable) {
		return roleRepo.findAllByStoreId(store_id, pageable);
	}

	@Override
	public RoleModel findRole(long id) {
		return roleRepo.findOne(id);
	}

	@Override
	public boolean existRole(String name, long store_id) {
		return roleRepo.existByName(name,store_id);
	}

	@Override
	public RoleModel updateRole(RoleForm roleForm) {
		RoleModel roleModel = roleRepo.findOne(roleForm.getId());
		roleModel.name = roleForm.name;
		roleModel.permissionIds = roleForm.permissionIds;
		roleRepo.save(roleModel);
		return roleModel;
	}

	@Override
	public void delRole(long id) {
		roleRepo.delete(id);
	}

	@Override
	public RoleModel addRole(RoleForm roleForm) {
		RoleModel roleModel = new RoleModel();
		roleModel.name = roleForm.name;
		roleModel.permissionIds = roleForm.permissionIds;
		roleModel.storeId = roleForm.storeId;
		roleModel.modifyFlag = roleForm.modifyFlag;
		roleRepo.save(roleModel);
		return roleModel;
	}

	@Override
	public void addDefaultRole(long store_id) {
		RoleModel waiter = new RoleModel();
		RoleModel cashier = new RoleModel();
		waiter.modifyFlag = 0;
		waiter.permissionIds = "";
		waiter.storeId = store_id;
		waiter.name = "服务员";

		cashier.modifyFlag = 0;
		cashier.permissionIds = "";
		cashier.storeId = store_id;
		cashier.name = "收银员";
		roleRepo.save(waiter);
		roleRepo.save(cashier);

	}

}
